using Microsoft.AspNetCore.Mvc;
using System.Configuration;
using static System.Runtime.InteropServices.JavaScript.JSType;


namespace UwinEducation.Shared.Models
{

    /// <summary>
    /// API统一返回格式
    /// </summary>
    /// <typeparam name="T">返回数据类型</typeparam>
    public class ApiResponse<T>
    {
        /// <summary>
        /// 静态版本号，在应用启动时设置
        /// </summary>
        public static string AppVersion { get; set; } = "1.0.0";

        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public string Version { get; set; } = AppVersion;

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success => Code == 200;


        public static ApiResponse<T> Ok(T data, string message = "操作成功")
        {
            return new ApiResponse<T>
            {
                Code = 200,
                Message = message,
                Data = data,
                Version = UwinEducation.Shared.Models.ApiResponse<object>.AppVersion
            };
        }

        public static ApiResponse<T> Error(string message, int code = 500)
        {
            return new ApiResponse<T>
            {
                Code = code,
                Message = message,
                Version = UwinEducation.Shared.Models.ApiResponse<object>.AppVersion
            };
        }
        public static ApiResponse<T> HandleBusinessException(T data, int code = 500)
        {
            return new ApiResponse<T>
            {
                Code = code,
                Data = data,
                Version = UwinEducation.Shared.Models.ApiResponse<object>.AppVersion
            };
        }
    }
}